//
// Created by xiexiangping on 23-04-17
//
#ifndef BASECOLLECTIONENCODE_H
#define BASECOLLECTIONENCODE_H

#include "tool/frameOperate.h"

class baseCollectionEncode {
public:

    baseCollectionEncode() {
        frameCount = 0;
        lastTime = (double) cv::getTickCount() / cv::getTickFrequency() * 1000; // ms
        streamIndex = -1;
    }

    ~baseCollectionEncode() {}

    static void registerDevice() {
        avdevice_register_all();            //注册所有可用的设备
        //avformat_network_init();
        av_log_set_level(AV_LOG_ERROR);

        for(std::string &s : devices) {
            std::cout << s << std::endl;
        }
    }

    //电脑设备数组
    static std::vector<std::string> devices;

    //枚举电脑设备
    static std::vector<std::string> enumAudioVideoDevices();

    void start() {
        this->thread = std::thread(&baseCollectionEncode::process, this);
    }

    void wait() {
        this->thread.join();
    }

protected:

    void process() {
        if(initStream())
            read();
    }

    virtual bool initStream() = 0;

    virtual void read() = 0;

    void calFrameFps() {

        ++frameCount;
        if (frameCount >= 10) // 取固定帧数为100帧
        {
            double curTime = (double) cv::getTickCount() / cv::getTickFrequency() * 1000;
            frameFps = frameCount / (curTime - lastTime) * 1000;
            lastTime = curTime;
            frameCount = 0;
        }
    }

    std::thread thread;
    AVFormatContext* fmtCtx;
    AVCodecContext* pCodecCtx;
    std::string device;
    float frameFps;
    int frameCount;
    int streamIndex;
    double lastTime;
};




#endif // BASECOLLECTIONENCODE_H
